use yew::prelude::*;

#[derive(Properties, PartialEq)]
pub struct Props {
  pub name: AttrValue,
  pub label: AttrValue,
  pub value: AttrValue,
  pub options: Vec<(AttrValue, AttrValue)>,
  pub onchange: Callback<Event>,
}

#[function_component]
pub fn Select(props: &Props) -> Html {
  let html_id = format!("select-{}", props.name);

  html! {
    <>
      <label for={html_id.clone()}>{props.label.clone()}</label>
      <select
        id={html_id}
        name={props.name.clone()}
        value={props.value.clone()}
        onchange={props.onchange.clone()}
      >
        {
          props.options.clone().into_iter().map(|opt| {
            html! {
              <option value={opt.0}>{opt.1}</option>
            }
          }).collect::<Html>()
        }
      </select>
    </>
  }
}
